﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrainManage.Server.Factory;
using GrainManage.Entities.Price;
using GrainManage.Data.Util;

namespace GrainManage.Server.Business
{
    class PriceBusiness : BaseBusiness, GrainManage.Interface.IPrice
    {

        //************************************************POST************************************************

        public OutputGetGrainType GetGrainType(InputGetGrainType input)
        {
            var result = new OutputGetGrainType();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.GrainTypes = service.GetGrainType(input);
                if (result.GrainTypes.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetPriceType GetPriceType(InputGetPriceType input)
        {
            var result = new OutputGetPriceType();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.PriceTypes = service.GetPriceType(input);
                if (result.PriceTypes.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetRecentPrice GetRecentPrice(InputGetRecentPrice input)
        {
            var result = new OutputGetRecentPrice();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.Prices = service.GetRecentPrice(input.PriceFilter, input, input.Authentication.UserName);
                if (result.Prices.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetPrice GetPrice(InputGetPrice input)
        {
            var result = new OutputGetPrice();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.Prices = service.GetPrice(input.PriceFilter, input, result, input.Authentication.UserName);
                if (result.Prices.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputInsert Insert(InputInsert input)
        {
            var result = new OutputInsert();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.PriceID = service.Insert(input.Price);
                if (result.PriceID > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.InsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputUpdate Update(InputUpdate input)
        {
            var result = new OutputUpdate();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                if (service.Update(input.Price) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.UpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputDelete Delete(InputDelete input)
        {
            var result = new OutputDelete();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                if (service.Delete(input.PriceID) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.DeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchInsert BatchInsert(InputBatchInsert input)
        {
            var result = new OutputBatchInsert();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.PriceIDs = service.Insert(input.Prices);
                if (!result.PriceIDs.Contains(0))
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchInsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchUpdate BatchUpdate(InputBatchUpdate input)
        {
            var result = new OutputBatchUpdate();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.InvalidPriceIDs = service.Update(input.Prices);
                if (result.InvalidPriceIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchUpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchDelete BatchDelete(InputBatchDelete input)
        {
            var result = new OutputBatchDelete();
            try
            {
                var service = ObjectFactory.GetPriceService(input);
                result.InvalidPriceIDs = service.Delete(input.PriceIDs);
                if (result.InvalidPriceIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchDeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }
    }
}
